/**
 * 豪车俱乐部
*/
module hcjlb {
    export class PlayMediatorHCJLB extends BaseMediator {
        public static NAME: string = "PlayMediatorHCJLB";
        private playerstep: Array<any> = [];
        public constructor(viewComponent: any = null) {
            super(PlayMediatorHCJLB.NAME, viewComponent);
        }
        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HCJLB.OPEN_PLAY_GAME,
                PanelNotify.HCJLB.CLOSE_PLAY_GAME,
                PanelNotify.HCJLB.JOIN_ROOM,

                PanelNotify.HCJLB.USER_BETS,
                PanelNotify.HCJLB.SCENE_INFO,
                PanelNotify.HCJLB.LOTTERY,
                PanelNotify.HCJLB.CONTINUE_CARD,
                PanelNotify.HCJLB.APPLY_BANKER,
                PanelNotify.HCJLB.BET_STATE,
                PanelNotify.HCJLB.LOTTERY_STATE,
                PanelNotify.HCJLB.FREE_STATE,
                PanelNotify.HCJLB.START_BET,
                PanelNotify.HCJLB.USER_INFO,
                PanelNotify.HCJLB.USER_SCORE,
                PanelNotify.HCJLB.CANCEL_CARD,
                PanelNotify.HCJLB.BANKER_INFO,
            ];
        }
        //面板
        private playPanel: PlayPanelHCJLB;
        private genzhuScore: number = 0;
        private t: number = null;
        private t2: number = null;
        private xiazhuTime = null;//下注间隔，后端快速下注会导致掉线，前段加个间隔
        private img_chip = [10, 100, 1000, 5000, 10000, 20000];
        private chipArr = [];//本局下的筹码
        private upChipArr = [];//上局下的筹码
        private titleX = 0;//本局中奖车标的坐标，最后动画用
        private titleY = 0;
        private titleSourec = "";
        private globLook: boolean = false;//结算过程金额不准变动
        public handleNotification(notification: puremvc.INotification): void {
            switch (notification.getName()) {
                case PanelNotify.HCJLB.CLOSE_PLAY_GAME: {
                    this.destroy();
                    break;
                };
                case PanelNotify.WJL.JOIN_ROOM:
                    if (this.playPanel && this.playPanel.parent) {
                        this.initUserInfo();
                    } else {//游戏中途加入有可能 先进玩家后加载房间数据，这时候面板还没起来。后台BUG
                        this.playPanel = null;
                        this.playPanel = new PlayPanelHCJLB();
                        this.showUI(this.playPanel, 2, 5);
                        game.AppFacade.instance.sendNotification(PanelNotify.HALL.JOIN_GAME_BY_HALL)
                    }
                    break;
                //玩家下注
                case PanelNotify.HCJLB.USER_BETS: {
                    this.userBets();
                    break;
                };
                //场景信息
                case PanelNotify.HCJLB.SCENE_INFO: {
                    this.scenneInfo();
                    break;
                };
                case PanelNotify.HCJLB.LOTTERY: {
                    this.lottery();
                    break;
                }
                //上庄列表
                case PanelNotify.HCJLB.CONTINUE_CARD: {
                    this.continueCard();
                    break;
                }
                //申请上庄
                case PanelNotify.HCJLB.APPLY_BANKER: {
                    this.applyBanker();
                    break;
                }
                //取消上庄
                case PanelNotify.HCJLB.CANCEL_CARD: {
                    this.cancelCard();
                    break;
                }
                //庄家信息
                case PanelNotify.HCJLB.BANKER_INFO: {
                    this.bankerInfo();
                    break;
                }
                //押注状态
                case PanelNotify.HCJLB.BET_STATE: {
                    this.betState();
                    break;
                }
                //开奖状态
                case PanelNotify.HCJLB.LOTTERY_STATE: {
                    this.lotteryState();
                    break;
                }
                //空闲状态
                case PanelNotify.HCJLB.FREE_STATE: {
                    this.freeState();
                    break;
                }
                //开始下注
                case PanelNotify.HCJLB.START_BET: {
                    this.startBet();
                    break;
                }
                //用户信息
                case PanelNotify.HCJLB.USER_INFO: {
                    this.userInfo();
                    break;
                }
                //用户分数
                case PanelNotify.HCJLB.USER_SCORE: {
                    this.userScore();
                    break;
                }


            }
        }
        public initUI(): void {
            if (InstructionsEnum.SOUND_GAME != InstructionsEnum.GAME_SOUND_NAME.HCJLB) {
                SoundManage.stopYinYue();
                InstructionsEnum.SOUND_GAME = InstructionsEnum.GAME_SOUND_NAME.HCJLB
                SoundManage.startYinYueLoad();
            }
            this.playPanel.out.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onExitBtnClick, this);
            this.playPanel.btn_rule.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRuleBtnCliCk, this);
            this.playPanel.btn_set.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSetBtnClick, this);
            this.playPanel.btn_continue.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onContinueBtnClick, this);
            this.playPanel.continue_card.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onContinueCard, this);
            this.playPanel.apply_banker.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onApplyBanker, this);
            this.playPanel.lottery_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onLotteryClose, this);
            for (let i = 1; i <= 8; i++) {
                this.playPanel["rect" + i].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRectClick, this);
                this.playPanel['lab_chiptext' + i].visible = false;
                this.playPanel['lab_my_bets' + i].visible = false;
            }
            // for (let i = 1; i <= 6; i++) {
            //     this.playPanel["img_chip" + i].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
            // }
            for (var i = 1; i <= 6; i++) {
                this.playPanel['img_chip' + i].source = 'bl' + i + '_new';
                this.playPanel['img_chip' + i].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
            }
            this.playPanel.btn_continue.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onContinueBtnClick, this);
            this.playPanel.btn_continue.source = "BCBM_btn_continue_n";
            //用户名
            this.playPanel.lab_nick_name.text = GameVO.getVo().szNickName + '';
            this.playPanel.img_headpic.source = "special_" + GameVO.getVo().wFaceID.toString();

            //隐藏闹钟
            this.playPanel.timeOut.x = -1200;

            this.playPanel.history.addEventListener(egret.Event.COMPLETE, () => {
                this.playPanel.history.verticalScrollBar = null;
                this.playPanel.history.removeEventListener(egret.Event.COMPLETE, () => {
                    this.playPanel.history.verticalScrollBar = null;
                }, this);
            }, this);
            this.playPanel.BCBM_gameing.visible = true;
        }
        /**
                * 初始化面板数据
                */
        public initData(): void {
            InstructionsEnum.isHall = true;
            this.globLook = false;
            this.clearData();
            this.initUserInfo();
        }
        //初始化页面数据
        private clearData() {
            this.globLook = false
            this.IsUserScore(0)
        }
        public destroy(): void {
            InstructionsEnum.isHall = false;
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.CLOSE_HELP);
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.CLOSE_USER_LIST);
            this.closePanel(1);
        }
        //玩家信息
        private initUserInfo() {
            //玩家信息
            // this.playPanel.userName.text = Global.cutString(GameVO.getVo().szNickName + "");
            // this.playPanel.userImg.source = "special_" + GameVO.getVo().wFaceID % 15;
            // this.playPanel.otherUserNum.text = Object.keys(HcjlbVO.getVo().userInfo).length + "" // Math.floor(100 + (Math.random() * 40)) + "";
            // if (this.globLook == false) {
            //     this.playPanel.userMoney.text = HcjlbVO.getVo().lUserScore / 100 + "";
            // }
        }
        //房间信息
        private initRoomInfo() {

        }



        //关闭开奖页面
        public onLotteryClose() {
            this.playPanel.lottery.x = -1200;
            this.playPanel.lottery.visible = false;
        }

        //用户分数
        public userScore() {
            if (GameVO.getVo().dwUserID == HcjlbVO.getVo().userid) {
                HcjlbVO.getVo().userGold = HcjlbVO.getVo().UserScore['lScore'];
                let nyum = ((HcjlbVO.getVo().UserScore['lScore'] / 100))
                let t = egret.setTimeout(() => {
                    this.playPanel.lab_iuser_score.text = nyum + "";
                }, this, 12000);
                InstructionsEnum.GAME_TIMEOUT_LIST.push(t);
                this.IsUserScore(this.playPanel.lab_iuser_score.text, false, true);
            }
        }
        //用户信息
        public userInfo() {
            if (HcjlbVO.getVo().tagMobileUserInfoHead[0]['dwUserID'] == GameVO.getVo().dwUserID) {
                try {
                    this.playPanel.lab_iuser_score.text = (GameVO.getVo().lUserScore / 100) + "";
                    HcjlbVO.getVo().userGold = GameVO.getVo().lUserScore;
                } catch (e) {
                    this.playPanel.lab_iuser_score.text = '0.00';
                }
                this.IsUserScore(this.playPanel.lab_iuser_score.text, false, true);

            }
        }
        public isStart = false;
        //开始下注
        public startBet() {
            this.ItemBetAmount = [0, 0, 0, 0, 0, 0, 0, 0]
            this.chipArr.splice(0, this.chipArr.length);
            this.isStart = true;
            Global.removeInterval();
            Global.removeTimeOut();
            this.startBetGold();
            this.IsUserScore(this.playPanel.lab_iuser_score.text, false, true);
            this.playPanel.btn_continue.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onContinueBtnClick, this);
            this.playPanel.btn_continue.source = "BCBM_btn_continue_h";
            this.playPanel.btn_continue.name = "";
            this.playPanel.BCBM_gameing.visible = false;
            this.playPanel.timeOut.x = 33;
            let time = HcjlbVO.getVo().dwTimeLeave / 1000;
            this.playPanel.timeOut.init(parseInt(time + ''));
            //连庄次数显示
            this.playPanel.BCBM_kaishi_num.text = HcjlbVO.getVo().lotterywBankOnJobTimes + "";
            egret.Tween.get(this.playPanel.BCBM_kaishi).to({ x: GameConfig.curWidth() / 2 }, 500, egret.Ease.sineOut).wait(1500).to({ x: 1500 }, 500, egret.Ease.sineOut).call(() => {
                egret.Tween.removeTweens(this.playPanel.BCBM_kaishi);
                this.playPanel.BCBM_kaishi.x = -400;
            });
            SoundManage.startYinxiaoLoad("sound-start-wager_mp3", 1);
            //刷新的时候隐藏了路单，在开新局显示出来
            this.playPanel.history_group.visible = true;
        }
        //开奖状态      
        private lotteryState() {
            this.playPanel.lottery.visible = false;
            this.playPanel.history_group.visible = false;//刷新回来先不显示路单，下局开始在现实
        }
        //空闲状态
        private freeState() {
            this.playPanel.lottery.visible = false;
        }
        //押注状态
        private betState() {
            this.ItemBetAmount = [0, 0, 0, 0, 0, 0, 0, 0];
            this.startBetGold();
            this.IsUserScore(this.playPanel.lab_iuser_score.text, false, true);
            this.playPanel.BCBM_gameing.visible = false;
            this.playPanel.timeOut.x = 33;
            let time = HcjlbVO.getVo().dwTimeLeave / 1000;
            this.playPanel.timeOut.init(parseInt(time + ''));
            for (var i = 0; i < HcjlbVO.getVo().sBetingItemInfo.length; i++) {
                if (HcjlbVO.getVo().sBetingItemInfo[i]['dwBetAmounts'] > 0) {
                    this.playPanel['lab_my_bets' + HcjlbVO.getVo().sBetingItemInfo[i]['wBetItem']].visible = true;
                    this.playPanel['lab_my_bets' + HcjlbVO.getVo().sBetingItemInfo[i]['wBetItem']].text = Number(HcjlbVO.getVo().sBetingItemInfo[i]['dwBetAmounts'] / 100).toFixed(1) + "";
                }
            }
            this.userBets();
        }
        //获取上庄列表
        private onContinueCard() {
            if (this.playPanel.up_list.visible) {
                this.playPanel.up_list.visible = false;
                return;
            }
            this.playPanel.up_list.visible = true;
            this.playPanel.up.play(0);
            SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HCJLB, RegDataHcjlb.CMD_GP_ContinueCard, {}, false);
        }
        //申请上庄 下庄
        public onApplyBanker() {
            if (this.playPanel.apply_banker.name != "1") {
                SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HCJLB, RegDataHcjlb.CMD_GP_ApplyBanker, {}, false);
                return;
            }
            SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HCJLB, RegDataHcjlb.CMD_GP_CancelBanker, {}, false);

        }
        //申请庄家
        public applyBanker() {
            this.playPanel.wApplyBankerNum.text = "申请人数:" + HcjlbVO.getVo().wApplyBankerNum;
            if (HcjlbVO.getVo().successful == 1) {
                //1:申请成功 0:金币不足 2-同一玩家不能多次申请 3-当前庄家不再接受申请
                EffectUtils.textFadeAway("申请成功");
                this.playPanel.apply_banker.name = "1";
                this.playPanel.apply_banker.source = "btn-qxsz@2x";
            }
            if (HcjlbVO.getVo().successful == 0) {
                EffectUtils.textFadeAway("金币不足");
                this.playPanel.apply_banker.name = "0";
                this.playPanel.apply_banker.source = "btn-sqsz@2x";

            }
            if (HcjlbVO.getVo().successful == 2) {
                EffectUtils.textFadeAway("您已申请上庄，请耐心等待");
                this.playPanel.apply_banker.name = "1";
                this.playPanel.apply_banker.source = "btn-qxsz@2x";

            }
            if (HcjlbVO.getVo().successful == 3) {
                EffectUtils.textFadeAway("当前庄家不再接受申请");
                this.playPanel.apply_banker.name = "0";
                this.playPanel.apply_banker.source = "btn-sqsz@2x";
            }
        }
        //取消上庄
        public cancelCard() {
            if (HcjlbVO.getVo().cancelCard['successful'] == 1) {
                //1:申请成功 0:金币不足 2-同一玩家不能多次申请 3-当前庄家不再接受申请
                EffectUtils.textFadeAway("取消成功");
                this.playPanel.apply_banker.source = "btn-sqsz@2x";
                this.playPanel.apply_banker.name = "0";
            }
            if (HcjlbVO.getVo().successful == 0) {
                EffectUtils.textFadeAway("取消失败");
            }
        }
        //返回上庄列表
        private continueCard() {
            this.playPanel.wApplyBankerNum.text = "申请人数:" + HcjlbVO.getVo().cbApplyNum;
            let sApplyBanker = HcjlbVO.getVo().sApplyBanker;
            for (var i = 0; i < 5; i++) {
                var y = i + 1;
                if (!Global.isEmpty(sApplyBanker[i].szNickName)) {
                    this.playPanel["up_img" + y].visible = true;
                    this.playPanel["up_name" + y].visible = true;
                    this.playPanel["up_gold" + y].visible = true;
                    this.playPanel["up_t" + y].visible = true;
                    this.playPanel["up_name" + y].text = sApplyBanker[i].szNickName;
                    this.playPanel["up_gold" + y].text = sApplyBanker[i].lScore ? sApplyBanker[i].lScore / 100 : '';
                } else {
                    this.playPanel["up_img" + y].visible = false;
                    this.playPanel["up_name" + y].visible = false;
                    this.playPanel["up_gold" + y].visible = false;
                    this.playPanel["up_t" + y].visible = false;
                }
            }
        }
        //选择筹码
        private onChipClick(event: egret.TouchEvent): void {
            for (var i = 1; i <= 6; i++) {
                if (this.playPanel["img_chip" + i] != event.currentTarget) {
                    egret.Tween.removeTweens(this.playPanel["img_chip" + i]);
                    this.playPanel["img_chip" + i].name = '';
                    this.playPanel["img_chip" + i].y = 620;
                } else {
                    if (event.currentTarget.name == 'ok') {
                        egret.Tween.removeTweens(event.currentTarget);
                        event.currentTarget.name = '';
                        event.currentTarget.y = 620;
                    } else {
                        event.currentTarget.name = 'ok';
                        egret.Tween.get(event.currentTarget).to({ y: 600 }, 200);
                    }
                }
            }
        }
        //点击下注
        // private myBettingItem;
        // private myBettingAmount;

        private onRectClick(event: egret.TouchEvent): void {
            let rec: eui.Rect = event.currentTarget;
            let chebiao: eui.Image = null;
            if (rec == this.playPanel.rect1) {
                chebiao = this.playPanel.chebiao_1;
            }
            if (rec == this.playPanel.rect2) {
                chebiao = this.playPanel.chebiao_2;
            }
            if (rec == this.playPanel.rect3) {
                chebiao = this.playPanel.chebiao_3;
            }
            if (rec == this.playPanel.rect4) {
                chebiao = this.playPanel.chebiao_4;
            }
            if (rec == this.playPanel.rect5) {
                chebiao = this.playPanel.chebiao_5;
            }
            if (rec == this.playPanel.rect6) {
                chebiao = this.playPanel.chebiao_6;
            }
            if (rec == this.playPanel.rect7) {
                chebiao = this.playPanel.chebiao_7;
            }
            if (rec == this.playPanel.rect8) {
                chebiao = this.playPanel.chebiao_8;
            }
            if (chebiao) {
                egret.Tween.get(chebiao).to({ scaleX: 1.2, scaleY: 1.2 }).wait(100).to({ scaleX: 1, scaleY: 1 }).call(() => {
                    egret.Tween.removeTweens(chebiao)
                }, this);
            }

            let imgNum;
            for (var i = 1; i <= 6; i++) {
                if (this.playPanel["img_chip" + i].name == 'ok') {
                    imgNum = this.img_chip[i - 1];
                }
            }
            if (!imgNum) {
                return;
            }
            let money = Number(this.playPanel.lab_iuser_score.text);
            if ((money * 100) < imgNum) {
                EffectUtils.textFadeAway("金币不足，请前往充值");
                return;
            }
            let obj = new Object();
            // this.myBettingItem = Number(event.currentTarget.name);
            // this.myBettingAmount = imgNum;
            let code = GameVO.getVo().dwUserID + "_" + event.currentTarget.name + "_" + imgNum;
            HcjlbVO.getVo().myBetts.push(code);
            obj['BettingItem'] = Number(event.currentTarget.name);
            obj['BettingAmount'] = imgNum;
            SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HCJLB, RegDataHcjlb.CMD_GP_PlaceJetton, obj, false);
        }

        //退出游戏
        private onExitBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);

            if (HcjlbVO.getVo().game_state == 1) {
                let obj = new Object();
                obj[InstructionsEnum.TEXT] = "请等待游戏结束后离开！";
                game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_TIP, obj);
            } else {
                let obj = new Object();
                obj[InstructionsEnum.CODE] = PanelNotify.HCJLB.CLOSE_PLAY_GAME;
                obj[InstructionsEnum.TEXT] = "确定要离开游戏吗？";
                game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_TIP, obj);
            }
        }

        //打开规则页面
        private onRuleBtnCliCk(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            game.AppFacade.instance.sendNotification(PanelNotify.HCJLB.OPEN_RULE);
        }

        //设置
        private onSetBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_SETTING);
        }

        //续投
        private onContinueBtnClick(event: egret.TouchEvent): void {

            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            if (this.playPanel.btn_continue.name == "ok") {
                EffectUtils.textFadeAway("本局已续投,不可重复续投");
                return;
            }
            if (this.upChipArr.length == 0) {
                EffectUtils.textFadeAway("上局未下注，无法投注");
                return;
            }
            if (this.isStart == false) {
                EffectUtils.textFadeAway("请等待下注时间");
                return;
            }
            this.playPanel.btn_continue.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onContinueBtnClick, this);
            this.playPanel.btn_continue.source = "BCBM_btn_continue_n"
            this.playPanel.btn_continue.name = "ok";//记录本局已点击过续投，下局开局时恢复
            let arr: Array<number> = [0, 0, 0, 0, 0, 0, 0, 0, 0];
            for (let i in this.upChipArr) {
                let amount = this.upChipArr[i].amount;
                let item = this.upChipArr[i].item;
                arr[item] += amount;
            }
            var fun: Function = function (time, item, amount) {
                let t = egret.setTimeout(function () {
                    var obj = new Object();
                    obj['BettingItem'] = item;
                    obj['BettingAmount'] = amount;
                    let code = GameVO.getVo().dwUserID + "_" + obj['BettingItem'] + "_" + obj['BettingAmount'];
                    HcjlbVO.getVo().myBetts.push(code);
                    SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HCJLB, RegDataHcjlb.CMD_GP_PlaceJetton, obj, false);
                }, this, time)
                InstructionsEnum.GAME_INTERVAL_LIST.push(t);
            }
            let time = 0;
            for (let i in arr) {
                if (arr[i] != 0) {
                    time += 200;
                    fun(time, i, arr[i])
                }
            }
        }
        //开奖跑马灯格数
        private RoundHighLight(num, i = 1, y = 1, n): void {
            if (num == 1 || i == 0 || y == 0) {
                this.titleX = this.playPanel["Logo_normol_" + (i - 1)].x;
                this.titleY = this.playPanel["Logo_normol_" + (i - 1)].y;
                this.titleSourec = this.playPanel["Logo_normol_" + (i - 1)].source;
                EffectUtils.Twinkle(this.playPanel["Logo_normol_" + (i - 1)], 2)
                return;
            }
            let t = egret.setTimeout(() => {
                let qs = this.lotteryNum(i);
                if ((y - 1) == 0) {
                    if ((i - 1) == 0) {
                        this.playPanel["Logo_normol_" + '32'].source = "Logo_normol_08";
                    } else {
                        this.playPanel["Logo_normol_" + (i - 1)].source = "Logo_normol_08";
                    }
                } else {
                    this.playPanel["Logo_normol_" + (i - 1)].source = "Logo_normol_0" + (y - 1);
                }
            }, this, 120);
            InstructionsEnum.GAME_TIMEOUT_LIST.push(t);
            if (i > 32) {
                i = 1;
            }
            if (y > 8) {
                y = 1;
            }
            this.playPanel["Logo_normol_" + i].source = "Logo1_0" + y;
            //低于8个格子时间变慢
            var time = 0;
            if (num < 8) {
                time = 500;
            } else {
                time = 20;
            }
            let t1 = egret.setTimeout(() => {
                this.RoundHighLight(--num, ++i, ++y, n);
            }, this, time);
            InstructionsEnum.GAME_TIMEOUT_LIST.push(t1);
        }


        //金币拆分
        private qiegeJinbi(fen: number) {
            let Fen = fen
            let imgList: Array<any> = [];
            let guigeList = [200, 100, 50, 10, 1, 0.1];
            for (let j = 0; j < guigeList.length; j++) {
                let len = Math.floor(Fen / guigeList[j]);
                if (len != 0) {
                    for (var k = 0; k < len; k++) {
                        imgList.push(guigeList[j]);
                        Fen -= guigeList[j];
                    }
                }
            }
            return imgList
        }
        //上次投注额度
        public ItemBetAmount = [0, 0, 0, 0, 0, 0, 0, 0]
        //玩家下注 
        public userBets() {
            let ItemBetAmount = HcjlbVO.getVo().ItemBetAmount;
            let wUserID = HcjlbVO.getVo().wUserID//下注的人
            let wBetItem = HcjlbVO.getVo().wBetItem//下到哪个位置
            let wBetAmount = HcjlbVO.getVo().wBetAmount;//下了多少钱
            let code = wUserID + "_" + wBetItem + "_" + wBetAmount;//此次下注唯一标识
            //看是自己下注还是其他玩家
            let is_my = false;//本次是否已经展示过我的筹码，若未展示就从所有筹码中匹配自己那一颗
            let flag = false;
            if (HcjlbVO.getVo().myBetts.indexOf(code) != -1) {
                HcjlbVO.getVo().myBetts.splice(HcjlbVO.getVo().myBetts.indexOf(code), 1)
                flag = true;
            }
            if (flag) {
                HcjlbVO.getVo().game_state = 1;
                HcjlbVO.getVo().userGold -= wBetAmount;
                GameVO.getVo().lUserScore -= wBetAmount;
                let myMoney = HcjlbVO.getVo().userGold / 100;
                this.playPanel.lab_iuser_score.text = myMoney + "";
                this.IsUserScore(myMoney, true);
                this.playPanel['lab_my_bets' + wBetItem].visible = true;
                this.playPanel['lab_chiptext' + wBetItem].visible = true;
                let num = Number(this.playPanel['lab_my_bets' + wBetItem].text);
                this.playPanel['lab_my_bets' + wBetItem].text = (num + wBetAmount / 100).toFixed(1) + "";
                //我的下注记录到续投
                let jbArr = {};
                jbArr['item'] = wBetItem;
                jbArr['amount'] = wBetAmount;
                this.chipArr.push(jbArr);
            }
            //总下注
            let sum_bets = 0;
            //刷新所有位置的钱
            for (var i = 0; i < ItemBetAmount.length; i++) {
                let wBetItems = ItemBetAmount[i]["wBetItem"];
                let dwBetAmounts = ItemBetAmount[i]["dwBetAmounts"];
                sum_bets += dwBetAmounts;
                this.playPanel['lab_chiptext' + wBetItems].visible = true;
                this.playPanel['lab_chiptext' + wBetItems].text = Number((dwBetAmounts / 100)).toFixed(1) + "";
                //该区域增长的金币
                let gold = dwBetAmounts - this.ItemBetAmount[i];
                //如果自己也在该区域下注，则从这一区域所有筹码中扣除自己下的筹码
                if (wBetItems == wBetItem && flag && Global.isNotEmpty(wBetAmount)) {
                    gold -= wBetAmount;
                    let goldMy = this.qiegeJinbi(wBetAmount / 100);
                    for (let y = 0; y < goldMy.length; y++) {
                        //如果这次自己也有下注，则从这一位置所有筹码中扣除自己下的筹码，从自己头像飞出，扣除所有自己的筹码后is_my设置为true后面的不再匹配
                        let startX = 116;
                        let startY = 664;
                        let tmp = new eui.Image();
                        tmp.source = "chips_" + goldMy[y] * 100 + "_new";
                        tmp.x = startX;
                        tmp.y = startY;
                        tmp.width = 30;
                        tmp.height = 30;
                        this.playPanel.rect.addChild(tmp);
                        var tmpX = this.playPanel["rect" + wBetItems].x + (this.playPanel["rect" + wBetItems].width - tmp.width) * Math.random();
                        var tmpY = this.playPanel["rect" + wBetItems].y + (this.playPanel["rect" + wBetItems].height - 45 - tmp.height) * Math.random();
                        egret.Tween.get(tmp).wait(this.random_num(50, 500)).to({ x: tmpX, y: tmpY }, 500, egret.Ease.circInOut).call((tmp: eui.Image) => {
                            egret.Tween.removeTweens(tmp);
                        }, this, [tmp]);
                    }
                }
                //其他人的金币
                if (gold > 0) {
                    let goldArr = this.qiegeJinbi(gold / 100);
                    for (let y = 0; y < goldArr.length; y++) {
                        let tmp = new eui.Image();
                        tmp.source = "chips_" + goldArr[y] * 100 + "_new";
                        tmp.x = 1120;
                        tmp.y = 565;
                        tmp.width = 30;
                        tmp.height = 30;
                        this.playPanel.rect.addChild(tmp);
                        var tmpX = this.playPanel["rect" + wBetItems].x + (this.playPanel["rect" + wBetItems].width - tmp.width) * Math.random();
                        var tmpY = this.playPanel["rect" + wBetItems].y + (this.playPanel["rect" + wBetItems].height - 45 - tmp.height) * Math.random();
                        egret.Tween.get(tmp).wait(this.random_num(50, 500)).to({ x: tmpX, y: tmpY }, 500, egret.Ease.circInOut).call((tmp: eui.Image) => {
                            egret.Tween.removeTweens(tmp);
                        }, this, [tmp]);
                    }
                }
            }
            this.playPanel.sum_bets.text = (sum_bets / 100).toString();
            this.ItemBetAmount = [];
            for (var i = 0; i < ItemBetAmount.length; i++) {
                this.ItemBetAmount.push(ItemBetAmount[i].dwBetAmounts);
            }
            SoundManage.startYinxiaoLoad("bcbm_ADD_GOLD_mp3", 1);
        }

        //场景信息
        private scroller: eui.Scroller;
        //庄家信息
        private bankerInfo() {
            if (HcjlbVO.getVo().dwUserID == GameVO.getVo().dwUserID) {
                this.playPanel.apply_banker.name = "0";
                this.playPanel.apply_banker.source = "btn-sqsz@2x";
            }
            if (this.playPanel.cBankName.text != HcjlbVO.getVo().cBankName) {
                SoundManage.startYinxiaoLoad("zhuang-2_mp3", 1);
                this.playPanel.BCBM_gameFlow.source = "BCBMhuanzhuang";
                this.playPanel.BCBM_gameFlow.anchorOffsetX = this.playPanel.BCBM_gameFlow.width / 2;
                egret.Tween.get(this.playPanel.BCBM_gameFlow).to({ x: GameConfig.curWidth() / 2 }, 500, egret.Ease.sineOut).wait(1000).to({ x: 1280 + 600 }, 500, egret.Ease.sineOut).call((obj: eui.Image) => {
                    egret.Tween.removeTweens(this.playPanel.BCBM_gameFlow)
                    this.playPanel.BCBM_gameFlow.x = -600;
                })
            }
            this.playPanel.cBankName.text = HcjlbVO.getVo().cBankName;
            this.playPanel.llBankerScore.text = (HcjlbVO.getVo().llBankerScore / 100).toString();
            this.playPanel.wApplyBankerNum.text = "申请人数:" + HcjlbVO.getVo().wApplyBankerNum;
            this.playPanel.BankerLowestProperty.text = "上庄需要：" + (HcjlbVO.getVo().BankerLowestProperty / 100);
        }
        //场景信息
        public scenneInfo() {
            this.playPanel.cBankName.text = HcjlbVO.getVo().cBankName;
            this.playPanel.llBankerScore.text = (HcjlbVO.getVo().llBankerScore / 100).toString();
            this.playPanel.wApplyBankerNum.text = "申请人数:" + HcjlbVO.getVo().wApplyBankerNum;
            this.playPanel.BankerLowestProperty.text = "上庄需要：" + (HcjlbVO.getVo().BankerLowestProperty / 100);
            for (var i = 0; i < HcjlbVO.getVo().wDrawRecord.length; i++) {
                var img: eui.Image;
                img = new eui.Image("Logo_normol_0" + this.lotteryNum(HcjlbVO.getVo().wDrawRecord[i]));
                img.width = 65;
                img.height = 65;
                img.y = i * 65;
                this.playPanel.history_group.addChild(img);
            }

        }
        //开奖
        public lottery() {
            this.globLook = false;
            if (this.chipArr.length == 0) {//若本局没有续投也没有手动下注，则下一次 上局筹码 为空
                this.upChipArr.splice(0, this.upChipArr.length);
            } else {
                this.upChipArr = [...this.chipArr];//本局有下注则记录为 “下一次 上局筹码 ” 上一局的不保留
            }
            this.isStart = false;
            //禁止点击下注
            for (var i = 1; i <= 6; i++) {
                egret.Tween.removeTweens(this.playPanel["img_chip" + i]);
                this.playPanel["img_chip" + i].name = '';
                this.playPanel["img_chip" + i].y = 620;
                this.playPanel['img_chip' + i].source = 'bl' + i + '_new';
                this.playPanel['img_chip' + i].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
            }

            SoundManage.startYinxiaoLoad("sound-end-wager_mp3", 1);
            this.playPanel.lottery.visible = true;
            this.playPanel.BCBM_gameFlow.source = "BCBM_tingzhixiazhu";
            this.playPanel.BCBM_gameFlow.x = -1000;
            this.playPanel.BCBM_gameFlow.$anchorOffsetX = this.playPanel.BCBM_gameFlow.width / 2;
            egret.Tween.get(this.playPanel.BCBM_gameFlow).to({ x: GameConfig.curWidth() / 2 }, 200, egret.Ease.sineOut).wait(500).to({ x: 1280 + 1000 }, 200, egret.Ease.sineOut).call((obj: eui.Image) => {
                obj.source = "";
                obj.x = 128;
            }, this, [this.playPanel.BCBM_gameFlow])
            this.playPanel.btn_continue.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onContinueBtnClick, this);
            this.playPanel.btn_continue.source = "BCBM_btn_continue_n"

            this.playPanel.timeOut.x = -1200;
            let num = 65 + HcjlbVO.getVo().lotterywWhichOne;
            let t2 = egret.setTimeout(() => {
                this.RoundHighLight(num, 1, 1, HcjlbVO.getVo().lotterywWhichOne);
            }, this, 2000);
            InstructionsEnum.GAME_TIMEOUT_LIST.push(t2);

            let PlayerInfoArray = HcjlbVO.getVo().lotteryPlayerInfoArray;
            this.playPanel.name1.text = PlayerInfoArray[0]['cPlayerName'];
            this.playPanel.name2.text = PlayerInfoArray[1]['cPlayerName'];
            this.playPanel.name3.text = PlayerInfoArray[2]['cPlayerName'];
            this.playPanel.gold1.text = (PlayerInfoArray[0]['llProfit'] ? PlayerInfoArray[0]['llProfit'] / 100 : '') + '';
            this.playPanel.gold2.text = (PlayerInfoArray[1]['llProfit'] ? PlayerInfoArray[1]['llProfit'] / 100 : '') + '';
            this.playPanel.gold3.text = (PlayerInfoArray[2]['llProfit'] ? PlayerInfoArray[2]['llProfit'] / 100 : '') + '';

            this.playPanel.z_name.text = HcjlbVO.getVo().cBankName;
            if (HcjlbVO.getVo().lotterydwBankerScore > 1) {
                this.playPanel.z_winning.text = '赢';
            } else if (HcjlbVO.getVo().lotterydwBankerScore == 0) {
                this.playPanel.z_winning.text = '未下注';
            } else {
                this.playPanel.z_winning.text = '输';
            }
            this.playPanel.z_gold.text = HcjlbVO.getVo().lotterydwBankerScore ? HcjlbVO.getVo().lotterydwBankerScore / 100 : HcjlbVO.getVo().lotterydwBankerScore;

            this.playPanel.i_name.text = GameVO.getVo().szNickName;
            if (HcjlbVO.getVo().lotteryllTotalPfofit > 1) {
                this.playPanel.i_winning.text = '赢';
            } else if (HcjlbVO.getVo().lotteryllTotalPfofit == 0) {
                this.playPanel.i_winning.text = '未下注';
            } else {
                this.playPanel.i_winning.text = '输';
            }
            this.playPanel.i_gold.text = HcjlbVO.getVo().lotteryllTotalPfofit ? HcjlbVO.getVo().lotteryllTotalPfofit / 100 : HcjlbVO.getVo().lotteryllTotalPfofit;
            var lotteryNum = HcjlbVO.getVo().lotterywWhichOne;
            if (lotteryNum % 8 == 1) {
                this.playPanel.logo.source = 'Logo_normol_01';
                this.playPanel.multiple.source = "bei20";
            }
            if (lotteryNum % 8 == 2) {
                this.playPanel.logo.source = 'Logo_normol_02';
                this.playPanel.multiple.source = "bei5";
            }
            if (lotteryNum % 8 == 3) {
                this.playPanel.logo.source = 'Logo_normol_03';
                this.playPanel.multiple.source = "bei30";
            }
            if (lotteryNum % 8 == 4) {
                this.playPanel.logo.source = 'Logo_normol_04';
                this.playPanel.multiple.source = "bei5";
            }
            if (lotteryNum % 8 == 5) {
                this.playPanel.logo.source = 'Logo_normol_05';
                this.playPanel.multiple.source = "bei40";
            }
            if (lotteryNum % 8 == 6) {
                this.playPanel.logo.source = 'Logo_normol_06';
                this.playPanel.multiple.source = "bei5";
            }
            if (lotteryNum % 8 == 7) {
                this.playPanel.logo.source = 'Logo_normol_07';
                this.playPanel.multiple.source = "bei10";
            }
            if (lotteryNum % 8 == 0) {
                this.playPanel.logo.source = 'Logo_normol_08';
                this.playPanel.multiple.source = "bei5";
            }

            //新的车标添加到车标集合最前
            HcjlbVO.getVo().wDrawRecord.pop();
            HcjlbVO.getVo().wDrawRecord.unshift(HcjlbVO.getVo().lotterywWhichOne);
            var t = egret.setTimeout(() => {
                this.playPanel.history_group.removeChildren();
                for (var i = 0; i < HcjlbVO.getVo().wDrawRecord.length; i++) {
                    var img: eui.Image;
                    img = new eui.Image("Logo_normol_0" + this.lotteryNum(HcjlbVO.getVo().wDrawRecord[i]));
                    img.width = 65;
                    img.height = 65;
                    img.anchorOffsetX = 33;
                    img.anchorOffsetY = 33;
                    img.y = i * 66;
                    this.playPanel.history_group.addChild(img);
                }
                this.playPanel.history_group.getChildAt(0).visible = false;
                this.playPanel.history_group.scrollV = this.playPanel.history_group.getChildAt(1).y;
                egret.Tween.get(this.playPanel.history_group).to({ scrollV: 0 }, 400).call(() => {
                    var img = new eui.Image(this.titleSourec);
                    img.anchorOffsetX = 60;
                    img.anchorOffsetY = 60;
                    img.x = this.titleX + 60;
                    img.y = this.titleY + 60;
                    this.playPanel.addChild(img);
                    egret.Tween.get(img).to({ x: 1235, y: 110 }, 300, egret.Ease.sineInOut).to({ scaleX: 1.3, scaleY: 1.3, alpha: 0 }, 300).call(() => {
                        egret.Tween.removeTweens(img);
                        if (this.playPanel.contains(img)) {
                            this.playPanel.removeChild(img);
                        }
                        this.playPanel.history_group.getChildAt(0).visible = true;
                        EffectUtils.Twinkle(this.playPanel.history_group.getChildAt(0), 3)

                        this.playPanel.lottery.x = -1000;
                        this.playPanel.lottery.anchorOffsetX = this.playPanel.lottery.width / 2;
                        egret.Tween.get(this.playPanel.BCBM_chip_highlight, { loop: true }).to({ rotation: -360 }).to({ rotation: 0 }, 10000);
                        egret.Tween.get(this.playPanel.lottery).to({ x: GameConfig.curWidth() / 2 }, 200, egret.Ease.sineOut).call(() => {
                            HcjlbVO.getVo().game_state = null;
                            if (HcjlbVO.getVo().lotteryllTotalPfofit > 1) {
                                SoundManage.startYinxiaoLoad("bcbm_Win_mp3", 1);
                            } else if (HcjlbVO.getVo().lotteryllTotalPfofit == 0) {
                                SoundManage.startYinxiaoLoad("bcbm_Win_mp3", 1);
                            } else {
                                SoundManage.startYinxiaoLoad("bcbm_Lost_mp3", 1);
                            }
                            for (var i = 1; i <= 32; i++) {
                                this.playPanel['Logo_normol_' + i].source = 'Logo_normol_0' + this.lotteryNum(i);
                            }
                        }).wait(8000).to({ x: 1280 + 1000 }, 300, egret.Ease.sineOut);
                    })
                    egret.Tween.removeTweens(this.playPanel.history_group)
                })
                for (var i = 1; i <= 8; i++) {
                    this.playPanel['lab_my_bets' + i].text = 0;
                    this.playPanel['lab_chiptext' + i].text = 0;
                    this.playPanel['lab_chiptext' + i].visible = false;
                    this.playPanel['lab_my_bets' + i].visible = false;
                }
                this.playPanel.rect.removeChildren();
            }, this, 9000);
            InstructionsEnum.GAME_TIMEOUT_LIST.push(t);
        }
        //开启下注金币监控事件
        public startBetGold() {
            for (let i = 1; i <= 6; i++) {
                this.playPanel["img_chip" + i].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                let num = 0;
                if (i == 1) {
                    num = 10;
                } else if (i == 2) {
                    num = 100;
                }
                else if (i == 3) {
                    num = 1000;
                }
                else if (i == 4) {
                    num = 5000;
                }
                else if (i == 5) {
                    num = 10000;
                }
                else if (i == 6) {
                    num = 20000;
                }
                this.playPanel['img_chip' + i].source = 'chips_' + num + '_new';
            }
            this.playPanel.btn_continue.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onContinueBtnClick, this);
            this.playPanel.btn_continue.source = "BCBM_btn_continue_h";
        }
        //更改金币按钮状态
        public IsUserScore(num, xiazhu: boolean = false, startXiazhu: boolean = false) {//剩余的钱，是否是下注后更新，是否是开局首次下注
            //庄家不允许下注
            if (HcjlbVO.getVo().cBankName == GameVO.getVo().szNickName) {
                for (var i = 1; i <= 6; i++) {
                    this.playPanel['img_chip' + i].source = 'bl' + i + '_new';
                    this.playPanel['img_chip' + i].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                    this.playPanel['img_chip' + i].name = '';
                    this.playPanel['img_chip' + i].y = 620;
                }
                return;
            }
            //找出上次选中的筹码，若本轮金币不足导致筹码被取消选择，则筹码自动选择更低的
            let img = null;
            let modify = false;
            for (var i = 1; i <= 6; i++) {
                if (this.playPanel['img_chip' + i].name == 'ok') {
                    img = this.playPanel['img_chip' + i];
                    break;
                }
            }
            if (Number(num) < 200) {
                this.playPanel.img_chip6.source = 'bl6_new';
                this.playPanel.img_chip6.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.img_chip6.name = '';
                this.playPanel.img_chip6.y = 620;
                if (img == this.playPanel.img_chip6) {
                    modify = true;
                }
            }
            if (Number(num) < 100) {
                this.playPanel.img_chip5.source = 'bl5_new';
                this.playPanel.img_chip5.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.img_chip5.name = '';
                this.playPanel.img_chip5.y = 620;
                if (img == this.playPanel.img_chip5) {
                    modify = true;
                }
            }
            if (Number(num) < 50) {
                this.playPanel.img_chip4.source = 'bl4_new';
                this.playPanel.img_chip4.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.img_chip4.name = '';
                this.playPanel.img_chip4.y = 620;
                if (img == this.playPanel.img_chip4) {
                    modify = true;
                }
            }
            if (Number(num) < 10) {
                this.playPanel.img_chip3.source = 'bl3_new';
                this.playPanel.img_chip3.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.img_chip3.name = '';
                this.playPanel.img_chip3.y = 620;
                if (img == this.playPanel.img_chip3) {
                    modify = true;
                }
            }
            if (Number(num) < 1) {
                this.playPanel.img_chip2.source = 'bl2_new';
                this.playPanel.img_chip2.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.img_chip2.name = '';
                this.playPanel.img_chip2.y = 620;
                if (img == this.playPanel.img_chip2) {
                    modify = true;
                }
            }
            if (Number(num) < 0.1) {
                this.playPanel.img_chip1.source = 'bl1_new';
                this.playPanel.img_chip1.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.img_chip1.name = '';
                this.playPanel.img_chip1.y = 620;
                if (img == this.playPanel.img_chip1) {
                    modify = true;
                }
            }
            if ((xiazhu && modify) || startXiazhu) {//因下注导致钱不够筹码更换 开局自动选择最大的
                if ((xiazhu && modify)) {
                    EffectUtils.textFadeAway("金币不足，已自动切换筹码");
                }
                if (this.playPanel.img_chip6.source == 'chips_20000_new') {
                    this.playPanel.img_chip6.y = 600;
                    this.playPanel.img_chip6.name = "ok";
                } else if (this.playPanel.img_chip5.source == 'chips_10000_new') {
                    this.playPanel.img_chip5.y = 600;
                    this.playPanel.img_chip5.name = "ok";
                } else if (this.playPanel.img_chip4.source == 'chips_5000_new') {
                    this.playPanel.img_chip4.y = 600;
                    this.playPanel.img_chip4.name = "ok";
                } else if (this.playPanel.img_chip3.source == 'chips_1000_new') {
                    this.playPanel.img_chip3.y = 600;
                    this.playPanel.img_chip3.name = "ok";
                } else if (this.playPanel.img_chip2.source == 'chips_100_new') {
                    this.playPanel.img_chip2.y = 600;
                    this.playPanel.img_chip2.name = "ok";
                } else if (this.playPanel.img_chip1.source == 'chips_10_new') {
                    this.playPanel.img_chip1.y = 600;
                    this.playPanel.img_chip1.name = "ok";
                }
            }
        }
        //开奖36个号码对应数字
        public lotteryNum(num) {
            if (num % 8 == 0) {
                return 8;
            } else {
                return num % 8;
            }
        }
        /**
 * 生成指定的随机数
 */
        private random_num(min: number, max: number) {
            let Range = max - min;
            let Rand = Math.random();
            return (min + Math.round(Rand * Range));
        }


    }
    function sleep(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return true;
        }
    }

}
